#ifndef DATABASE_H
#define DATABASE_H

#include "precomp.h"

//#include <QObject>
//#include <QSqlDatabase>
//#include <QSqlError>
//#include <QSqlQueryModel>
//#include <QSqlQuery>
//#include <QStandardItemModel>
//#include <QSqlTransaction>
#include "publicstruct.h"

class DbManager : public QObject
{
    Q_OBJECT
public:
    explicit DbManager(QObject *parent = nullptr);
    DbInfo mySqlInfo;
    DbInfo sqlLiteInfo;
    QSqlDatabase m_mysqlConn,m_sqliteConn;

signals:

public slots:
    bool initMysql();           //初始化数据库连接
    bool initSqlite();          //初始化sqlite数据库连接
    bool initCfgData();         //读取sqlite中的配置信息

public:
    int  getRecTotalCount(QString qryType,QString qryParam);         //获取记录总数
    QString getParentPath(int parentId);
    QList<DomainInfo> getAllDomainRecByCategoryId(int categoryId,int curPage,int pageSize);
    QVector<DataDict> getDataDict();
    QList<DnSummaryInfo> getDomainRecByDnIds(QString  dnIds );
    QList<DnSummaryInfo> getDomainRecByCategoryId(int categoryId,QString filterSuffix,QString filterDnStatus,QString filterExpDate,QString filterRegStatus);
    QList<KeyValue> getDomainRegistryById(int domainId);
    QList<KeyValue> getDomainAdminById(int domainId);
    QList<KeyValue> getDomainTechById(int domainId);
    QList<KeyValue> getDomainBillById(int domainId);
    QVector<QString> getDomainStatusById(int domainId);
    QVector<QString> getDomainNameServerById(int domainId);
    QVector<ProxyServerData> getProxyServers(int proxyStatus);
    bool getWhoisServerByDomainSuffix(QVector<DomainQryAddrInfo> &suffixQryAddr);
    bool getWhoisServerMappingDnSuffix(QMap<QString ,QStringList> &dnSuffixMappingWhoIs);



    void loadCategoryTree(QStandardItemModel *model);// 从数据库加载树节点到模型

    bool insertDomainRecToTables(int categoryId,QStringList domainStrings);
    bool insertDomainRecToSingleTables(int categoryId,QString dnStr);
    bool insertBatchDomainRecToSingleTables(int categoryId,QStringList dnStrs);
    int insertCategoryNode(const QString nodePath,const QString nodeName);
    bool insertDomainRegistry(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool insertDomainAdmin(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool insertDomainTech(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool insertDomainBill(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool insertDomainStatus(int domainId,QString domainName,QVector<QString> statusVectors);
    bool insertDomainNameServer(int domainId,QString domainName,QVector<QString> nameServerVectors);


    bool deleteDomainRecFromTable(int domainId);
    bool deleteInvalidProxy();
    void deleteCategoryNode(int nodeId);
    bool deleteDomainRecFromTable(QVariantList domainIds);

    void updateCategoryNode(int nodeId,QString nodeName);
    bool updateDomainRegistryById(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool updateDomainAdminById(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool updateDomainTechById(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool updateDomainBillById(int domainId,QString domainName,QMap<QString,QString> infoMaps);
    bool updateDomainDetailInfoById(int domainId,QString domainName,DomainInfo dnInfo);
    bool updateProxyServerTable(QVector<ProxyServerData> &proxyServer);
    bool updateProxyServerStatus(QString proxyhost,QString updatedatetime,QString retStr);
    bool updateDnCategory(int categoryId,QString domainIds);
    bool updateDataDict(QString dd_key,DataDict dd);
};


#endif // DATABASE_H
